package com.zh.yqbj.exceptionHandler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.util.SaResult;
import com.zh.yqbj.exception.accountExcption.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static cn.dev33.satoken.exception.NotLoginException.NOT_TOKEN_MESSAGE;


@Slf4j
@RestControllerAdvice
@ResponseBody
public class AccountExceptionHandler {
    /**
     * 密码错误
     *
     * @param e 密码错误
     * @return 错误信息
     */
    @ExceptionHandler(ErrorPasswordException.class)
    public SaResult errorPasswordException(ErrorPasswordException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 账号不存在
     *
     * @param e 账号不存在
     * @return 错误信息
     */
    @ExceptionHandler(UnknownAccountException.class)
    public SaResult unknownAccountException(UnknownAccountException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 短信发送失败 余额不足
     *
     * @param e 余额不足
     * @return 错误信息
     */
    @ExceptionHandler(NoMoneyException.class)
    public SaResult noMoneyException(NoMoneyException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 验证码错误
     *
     * @param e 验证码错误
     * @return 错误信息
     */
    @ExceptionHandler(ErrorCodeException.class)
    public SaResult errorCodeException(ErrorCodeException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 手机号错误
     *
     * @param e 手机号错误
     * @return 错误信息
     */
    @ExceptionHandler(ErrorPhonePatternException.class)
    public SaResult errorPhonePatternException(ErrorPhonePatternException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 邮箱错误
     *
     * @param e 邮箱错误
     * @return 错误信息
     */
    @ExceptionHandler(ErrorEmailPatternException.class)
    public SaResult errorEmailPatternException(ErrorEmailPatternException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 账号格式错误
     *
     * @param e 账号格式错误
     * @return 错误信息
     */
    @ExceptionHandler(ErrorUsernamePatternException.class)
    public SaResult errorUsernamePatternException(ErrorUsernamePatternException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 两次密码不一致
     *
     * @param e 两次密码不一致
     * @return 错误信息
     */
    @ExceptionHandler(ErrorRepasswordException.class)
    public SaResult errorRepasswordException(ErrorRepasswordException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 该账号已存在
     *
     * @param e 该账号已存在
     * @return 错误信息
     */
    @ExceptionHandler(DuplicateAccountException.class)
    public SaResult duplicatedAccountException(DuplicateAccountException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 该邮箱已存在
     *
     * @param e 该邮箱已存在
     * @return 错误信息
     */
    @ExceptionHandler(DuplicateEmailException.class)
    public SaResult duplicatedEmailException(DuplicateEmailException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 该手机号已存在
     *
     * @param e 该手机号已存在
     * @return 错误信息
     */
    @ExceptionHandler(DuplicatePhoneException.class)
    public SaResult duplicatedPhoneException(DuplicatePhoneException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 注册失败
     *
     * @param e 注册失败
     * @return 错误信息
     */
    @ExceptionHandler(RregisterException.class)
    public SaResult registerException(RregisterException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 未绑定的手机号
     *
     * @param e 未绑定的手机号
     * @return 错误信息
     */
    @ExceptionHandler(UnBindingPhoneException.class)
    public SaResult unBindingPhoneException(UnBindingPhoneException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 未绑定的邮箱
     *
     * @param e 未绑定的邮箱
     * @return 错误信息
     */
    @ExceptionHandler(UnBindingEamilException.class)
    public SaResult unBindingEmailException(UnBindingEamilException e) {
        log.error("{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 未登录
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NotLoginException.class)
    public SaResult notLoginException(NotLoginException e) {
        log.info("{}", e.getMessage());
        SaResult saResult = new SaResult(401, "请登录", null);
        return saResult;
    }
}
